<%- include("header",{type:'create'}) %>
<div class="container">
	<form style="width:100%" method="post" class="form create">
		<div>
			<label>標題：</label>
			<input placeholder="請輸入標題" type="text" name="title">
		</div>
		<div>
			<label>内容：</label>
			<textarea placeholder="請輸入内容" name="content" id="" cols="42" rows="10"></textarea>
		</div>
		<div class="submit">發表</div>
	</form>
</div>
<script>
    // .submit是取得 class名稱為submit的名稱，如是id名的話要用 #submitto(<div id=submitto ...)
    $('.submit').click(function(){
        if ($('input[name=title]').val().trim() == '') {
            // 
            fade('請輸入標題')
        }else if ($('textarea').val().trim() == '') {
            fade('請輸入内容')
        }else{          
            $.ajax({
                url: "/create",
                data: $('.form').serialize(),
                type: "POST",
                cache: false,
                dataType: 'json',
                success: function (msg) {
                    if (msg.code == 200) {
                        fade('發表成功')
                        setTimeout(function(){
                            location.href = "/posts"
                        },1000)
                    }else{
                        fade('發表失敗')
                    }
                },
                error: function () {
                    alert('異常');
                }
            })			
        }   
    })
</script>
<% include footer %>